﻿//var dojoConfig = { parseOnLoad: true };
//dojo.require("esri.map");
//dojo.require("dijit.form.Button");
//dojo.require("dijit.layout.BorderContainer");
//dojo.require("dijit.layout.ContentPane");
//dojo.require("esri.dijit.BasemapGallery");
//dojo.require("esri.arcgis.utils");

//dojo.require("esri.layers.KMLLayer");
//dojo.require("dojox.mobile");


var map, GeoMarker;
var elevator;
var height = "";
var geocoder;
var Long = 100.540094, Lat = 13.770899;
var gmap = null;
var weatherLayer, trafficLayer;
var markersArray = [];
var proxy = "./cgi-bin/proxy.php?";			//proxy for camera reporter kml links
var publicwater, effectivearea, cloudLayer;
var branch_prev; // store current branch kml layer for radio button removal
var engineering = {		//engineering kml links
	"0101": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/0101.kmz", {preserveViewport: false})
	},
	"futurepipes": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/future_pipe.kmz", {preserveViewport: true})
	},
	"dma":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/DMA.kmz", {preserveViewport: true})
	},
	"region1":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Region1.kmz", {preserveViewport: true})
	},
	"region2":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Region2.kmz", {preserveViewport: true})
	},
	"region3":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Region3.kmz", {preserveViewport: true})
	},
	"region4":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Region4.kmz", {preserveViewport: true})
	},
	"branch": {
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Service/branch.kmz", {preserveViewport: false})
	},
	"servicearea": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Service/service_area.kmz", {preserveViewport: true})
	},
	"replacemain":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Replace_Main.kmz", {preserveViewport: false})
	},
	"ACPipe":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/AC_dis_pipe.kmz", {preserveViewport: false})
	},
	"stolenmeter":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/Engineer/Stolen_meter2.kmz", {preserveViewport: false})
	}
	
};
var waterresource = {		//water resource kml links
	"rainaccumulation3hr": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/3B42_rain_accumulation_3hr.kml", {preserveViewport: true})
	},
	"rainaccumulation72hr": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/3B42_rain_accumulation_72hr.kml", {preserveViewport: true})
	},
	"waterlevel": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/Thailand%20Water-level%20Telemetry.kmz", {preserveViewport: true})
	},
	"waterlevel1": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/Thailand%20Water-level%20Telemetry1.kmz", {preserveViewport: true})
	},
	"riverstation":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/river_station.kmz", {preserveViewport: true})
	},
	"earthquakes": 
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/USGS%20Earthquakes.kmz", {preserveViewport: true})
	},
	"autoweather":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/AWS-Station.kmz", {preserveViewport: true})
	},
	"bmaroadproject":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/BMA_road_project.kml", {preserveViewport: true})
	},
	"ngv":
	{
		"obj" : new google.maps.KmlLayer("http://115.31.184.241/KML/WaterResource/ngv.kmz", {preserveViewport: true})
	}
	
	
};
var serviceR = 		// GIS camera reporter kml links
[
		["mainleak", false, "http://gisonline.mwa.co.th/sveng/index.php?r=survey/kml&id=6"],
		["connect", false, "http://gisonline.mwa.co.th/sveng/index.php?r=survey/kml&id=4"],
		["engproj", false, "http://gisonline.mwa.co.th/sveng/index.php?r=survey/kml&id=14"],
		["PR", false, "http://gisonline.mwa.co.th/sveng/index.php?r=survey/kml&id=13"],
		["mainraise", false, "http://gisonline.mwa.co.th/sveng/index.php?r=survey/kml&id=12"]
];
var branch_array = {		//เส้นท่อตามสาขา kml links
	"01":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0101.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0102.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0103.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0104.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0105.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0106.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0107.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0108.kmz", { preserveViewport: false } )
	],
	"02":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0201.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0202.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0203.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0204.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0208.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0212.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0213.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0214.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0215.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0216.kmz", { preserveViewport: false } )
	],
	"03":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0301.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0302.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0303.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0304.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0305.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0306.kmz", { preserveViewport: false } )
	],
	"04":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0403.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0405.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0406.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0408.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0409.kmz", { preserveViewport: false } )
	],
	"05":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0501.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0502.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0503.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0504.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0505.kmz", { preserveViewport: false } )
	],
	"06":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0601.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0602.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0603.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0604.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0605.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0606.kmz", { preserveViewport: false } )
	],
	"07":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0701.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0703.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0704.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0705.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0706.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/0707.kmz", { preserveViewport: false } )
	],
	"11":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1101.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1103.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1105.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1106.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1107.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1108.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1110.kmz", { preserveViewport: false } )
	],
	"12":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1201.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1202.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1203.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1204.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1205.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1206.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1207.kmz", { preserveViewport: false } )
	],
	"13":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1301.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1302.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1303.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1304.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1305.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1306.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1307.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1308.kmz", { preserveViewport: false } )
	],
	"14":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1401.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1402.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1403.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1404.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1405.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1406.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1407.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1408.kmz", { preserveViewport: false } )
	],
	"15":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1501.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1502.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1503.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1504.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1505.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1506.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1507.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1508.kmz", { preserveViewport: false } )
	],
	"16":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1601.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1602.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1603.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1604.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1605.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1606.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1607.kmz", { preserveViewport: false } )
	],
	"17":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1701.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1702.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1703.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1704.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1705.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1706.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1707.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1708.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1709.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1710.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/1711.kmz", { preserveViewport: false } )
	],
	"53":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5301.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5302.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5303.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5304.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5305.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5306.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5308.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5309.kmz", { preserveViewport: false } )
	],
	"54":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5401.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5402.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5403.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5404.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5405.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5406.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5407.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5408.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5409.kmz", { preserveViewport: false } )
	],
	"55":
	[
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5501.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5502.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5503.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5504.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5505.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5506.kmz", { preserveViewport: false } ),
		new google.maps.KmlLayer( "http://gisonline.mwa.co.th/KML/Engineer/5507.kmz", { preserveViewport: false } )
	]
};




function init() {
    //esri.config.defaults.io.proxyUrl = "proxy/proxy.php";
    //esri.config.defaults.io.alwaysUseProxy = "false";
    geocoder = new google.maps.Geocoder(); //geocoder for google geolocation search
    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(Lat, Long),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        //ROADMAP, HYBRID, TERRAIN, SATELLITE
        mapTypeControl: false,
        panControl: false,
        zoomControl: true,
        streetViewControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.DEFAULT,
            position: google.maps.ControlPosition.LEFT_CENTER
        }
        //streetViewControl : false //enable/disable Google street view 
    };
    //google map
    map = new google.maps.Map(document.getElementById('map'), mapOptions); //show base map
    //open street basemap
    map.mapTypes.set("OSM", new google.maps.ImageMapType({
        getTileUrl: function(coord, zoom) {
            return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
        },
        tileSize: new google.maps.Size(256, 256),
        name: "OpenStreetMap",
        maxZoom: 18
    }));
    elevator = new google.maps.ElevationService(); //google elevation service
    // esri world map
    var url1 = 'http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/';
    var agsType1 = new gmaps.ags.MapType(url1, {
        name: 'ArcGIS'
    });
    map.mapTypes.set('esri', agsType1);
    // MWA 1:1000 cache map
    var url = './cgi-bin/ba-simple-proxy.php?url=http://172.16.195.14/ArcGIS/rest/services/mwa_backup_cache/MapServer';
    var cache = new gmaps.ags.MapType(url, {
        name: 'Cache'
    });
    map.mapTypes.set('cache', cache);
   
    var options = {
        center: [Long, Lat],
        basemap: "streets",
        // The following are valid options: "streets" , "satellite" , "hybrid", "topo", "gray", "oceans", "national-geographic". As of version 3.3
        zoom: 8
    }
    //get elevation and longlat from mouse
    google.maps.event.addListener(map, 'click', function(event) {
        var positionalRequest = {
            'locations': [event.latLng]
        }
        getElevation(positionalRequest);
        long3 = event.latLng.lng().toFixed(3);
        lat3 = event.latLng.lat().toFixed(3);
        $("#longlatlabel").html(long3 + ", " + lat3 + height);
    });
    google.maps.event.addListener(map, 'mousemove', function(event) {
        var positionalRequest = {
            'locations': [event.latLng]
        }
        getElevation(positionalRequest);
        long3 = event.latLng.lng().toFixed(3);
        lat3 = event.latLng.lat().toFixed(3);
        $("#longlatlabel").html(long3 + ", " + lat3 + height);
    });
	

    weatherLayer = new google.maps.weather.WeatherLayer({
        temperatureUnits: google.maps.weather.TemperatureUnit.CELCIUS
    });
    weatherLayer.setMap(map);
    //google cloud layer
    cloudLayer = new google.maps.weather.CloudLayer();
    //google traffic layer
    trafficLayer = new google.maps.TrafficLayer();
    //current user online count
    UserOnlineCount();
    //GIS camera reporter refresh 
    var t = setInterval("refreshKml()", 20000); //refresh every 2min
    
}
//display kml from link for GIS Camera reporter kml links
function displayKml(selected) {
	console.log(selected);
	if(selected == 'mainleak')		
	{
	
		selectedLayer = serviceR[0];
		selectedLayer[1] = true;
		path = selectedLayer[2];
		geo1 = new geoXML3.parser({
			map: map,
			zoom: true,
			singleInfoWindow: true
			//infoWindow: infowindow
			//createMarker: createMarker 
		});
		selectedLayer[3] = geo1;
		selectedLayer[3].parse(path);
	}
	if(selected == 'connect')
	{
		selectedLayer = serviceR[1];
		selectedLayer[1] = true;
		path = selectedLayer[2];
		geo2 = new geoXML3.parser({
			map: map,
			zoom: true,
			singleInfoWindow: true
			//infoWindow: infowindow
			//createMarker: createMarker 
		});
		selectedLayer[3] = geo2;
		selectedLayer[3].parse(path);
	}
	if(selected == 'engproj')		
	{
	
		selectedLayer = serviceR[2];
		selectedLayer[1] = true;
		path = selectedLayer[2];
		geo3 = new geoXML3.parser({
			map: map,
			zoom: true,
			singleInfoWindow: true
			//infoWindow: infowindow
			//createMarker: createMarker 
		});
		selectedLayer[3] = geo3;
		selectedLayer[3].parse(path);
	}	
	if(selected == 'PR')		
	{
	
		selectedLayer = serviceR[3];
		selectedLayer[1] = true;
		path = selectedLayer[2];
		geo4 = new geoXML3.parser({
			map: map,
			zoom: true,
			singleInfoWindow: true
			//infoWindow: infowindow
			//createMarker: createMarker 
		});
		selectedLayer[3] = geo4;
		selectedLayer[3].parse(path);
	}	
	if(selected == 'mainraise')		
	{
		selectedLayer = serviceR[4];
		selectedLayer[1] = true;
		path = selectedLayer[2];
		geo5 = new geoXML3.parser({
			map: map,
			zoom: true,
			singleInfoWindow: true
			//infoWindow: infowindow
			//createMarker: createMarker 
		});
		selectedLayer[3] = geo5;
		selectedLayer[3].parse(path);
	}	
}
//hide kml from link for GIS Camera reporter kml links
 function hideKml(selected){
	switch(selected)
	{
	case 'mainleak':
		serviceR[0][1] = false;
		serviceR[0][3].hideDocument();  // see geoxml3-modify: http://geocontext.org/pliki/2010/test-geoxml3/test2/geoxml3-modify.js
		break;
	case 'connect':
		serviceR[1][1] = false;
		serviceR[1][3].hideDocument(); 
		break;
	case 'engproj':
		serviceR[2][1] = false;
		serviceR[2][3].hideDocument(); 
		break;
	case 'PR':
		serviceR[3][1] = false;
		serviceR[3][3].hideDocument(); 
		break;
	case 'mainraise':
		serviceR[4][1] = false;
		serviceR[4][3].hideDocument(); 
		break;
	}			
}
//get current elevation from google
function getElevation(positionalRequest){
	elevator.getElevationForLocations(positionalRequest, function(results, status) {
		if (status == google.maps.ElevationStatus.OK) {
			// Retrieve the first result
			if (results[0]) {
				height = ", " + results[0].elevation.toFixed(3) + " m";
			} 
		} 
	});
}
//current user online count
function UserOnlineCount(){
	//console.log("counting users");
	var StartUserOnline = 0 
	if(StartUserOnline == 0)
	{                                                                 
		$.ajax({                    
			type: "POST",
			url: "UserOnline.php",                   
			data:{                            
				USER_ONLINE:"StartWebPage"                                                
			},                                   
			
			}
		).done(function(data) {                                                          
			$("#UserOnline").html(data);
			StartUserOnline = 1;
			UserOnlineRealtime();                    
		}); 
	}
	function UserOnlineRealtime(){
		$(function(){                
			setInterval(function(){                 
				var getData=$.ajax({ 
					type: "POST",
					url:"UserOnline.php",
					data: {USER_ONLINE:"StartWebPage"} ,
					async:false,
					success:function(getData){
						$("#UserOnline").html(getData); 
					}
				}).responseText;
			},120000) //refresh user count every 2 minute
		});
	}      
}
//Geolocation 

function checkGeolocationSupport(){
	// Check if the browser supports geolocation
	if(navigator.geolocation) {
	
		GeoMarker = new GeolocationMarker();
        GeoMarker.setCircleOptions({fillColor: '#808080'});
		

        google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
			map.setCenter(this.getPosition());
			map.fitBounds(this.getBounds());
			console.log(this.getAccuracy());
        });

        google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
			alert('There was an error obtaining your position. Message: ' + e.message);
        });
        GeoMarker.setMap(map);
		
		
	} 
	else {
		
	alert('The browser does not support geolocation');
		
	}
}
//clear current markers from map
function clearOverlays() {
	for (var i = 0; i < markersArray.length; i++ ) {
		markersArray[i].setMap(null);
	}
	markersArray = [];
}
//resize map to fit screen on orientation change
function orientationChanged() {
	hideAddressBar();
	if (isiPhone != null) {
		// iPhone
		if (isPortrait) {
			// portrait ----> landscape mode
			addjustMapHeight();
			isPortrait = false;
		} 
		else {
			// landscape ----> portrait mode
			dojo.byId("map").style.height = (window.innerHeight + statusBarHeight) + "px";
			isPortrait = true;
		}
		} 
		else if (isiPad != null) {
			// iPad
			addjustMapHeight();
		}
		if (map) {
			map.resize();
			map.reposition();
		}
}
function addjustMapHeight() {
	dojo.byId("map").style.height = (window.innerHeight - headerGeom.h) + "px";
}
function hideAddressBar() {
	// Set a timeout...
	setTimeout(function () {
		// Hide the address bar!
		window.scrollTo(0, 1);
	}, 0);
}

function currentPositionCallback(position) {
	// Create a new latlng based on the latitude and longitude from the user's position
	var user_lat_long = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
	// Add a marker using the user_lat_long position
	var marker = new google.maps.Marker({
		position: user_lat_long,
		map: map
	});
	markersArray.push(marker);
	// Set the center of the map to the user's position and zomm into a more detailed level
	map.setCenter(user_lat_long);
	map.setZoom(15);
}

function hideAddressBar() {
	if (navigator.userAgent.match(/Android/i)) {
		window.scrollTo(0, 0); // reset in case prev not scrolled
		var nPageH = $(document).height();
		var nViewH = window.outerHeight;
		if (nViewH > nPageH) {
			nViewH = nViewH / window.devicePixelRatio;
			$('BODY').css('height', nViewH + 'px');
		}
		window.scrollTo(0, 1);
	} else {
		addEventListener("load", function() {
			setTimeout(hideURLbar, 0);
			setTimeout(hideURLbar, 500);
		}, false);
	}

	function hideURLbar() {
		if (!pageYOffset) {
			window.scrollTo(0, 1);
		}
	}
	return this;
}
//close search pop
function closePopup(){
	$("#search_popup").popup( "close" );
}
//hide and display kml to refresh
function refreshKml() {
	for(var i = 0; i < serviceR.length; i++)
	{
		if(serviceR[i][3] != null){
			serviceR[i][3].hideDocument();
		}
	}
	for(var i = 0; i < serviceR.length; i++)
	{
		if(serviceR[i][1] == true){
			displayKml(serviceR[i][0]);
		}
	}
}
$(document).ready(function() { 
	function detectEvent(e) {
		//console.log('Current Event is: ' + e.type);
	}
	// select basemap
	$( "input.popup_basemap2" ).on( "click", function() {		
		//console.log("clicked");
		//console.log( $("input.popup_basemap2:checked").val());
		var selected = $("input.popup_basemap2:checked").val();
		if (selected == 'hybrid'){
			map.setMapTypeId(google.maps.MapTypeId.HYBRID);
		}
		if (selected == 'streets'){
			map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
		}
		if (selected == 'openstreet'){
			map.setMapTypeId("OSM");
		}
		if (selected == 'esri'){
			map.setMapTypeId('esri');
		}
		if (selected == 'cache'){
			map.setMapTypeId('cache');
		}
	});
	$('#geolocation').click(  function(){
		checkGeolocationSupport();
	});
	$( "input.engineering_kml" ).on( "click", function() {  		//turn engineering layer on/off
		var selectedLayer = $(this).val();
		//console.log(selectedLayer);
		if($(this).prop("checked"))  //add layer to map
		{
			if (selectedLayer == 'publicwater')						// call google maps ultility to overlay cache map service
			{
				map.setZoom(10);
				var url = 'http://172.16.195.14/ArcGIS/rest/services/Public_water/MapServer';
				publicwater = new gmaps.ags.MapOverlay(url);	
				publicwater.setMap(map);						
			}
			else													// display kml
				engineering[selectedLayer].obj.setMap(map);	
		}
		else  														//remove layer from map
		{
			if (selectedLayer == 'publicwater')
				publicwater.setMap(null);
			else
				engineering[selectedLayer].obj.setMap(null);
		}
		//var selected = $("input.popup_basemap2:checked").val();
		
		
	});
	$( "input.waterresource_checnbox" ).on( "click", function() {		//turn water resource layer on/off
		var selectedLayer = $(this).val();
		if (selectedLayer == "waterlevel")								// display 2 kmls for waterlevel 
		{	
			if($(this).prop("checked")){
				waterresource["riverstation"].obj.setMap(map);
				waterresource["waterlevel"].obj.setMap(map);
				waterresource["waterlevel1"].obj.setMap(map);
			}
			else
			{
				waterresource["riverstation"].obj.setMap(null);
				waterresource["waterlevel"].obj.setMap(null);
				waterresource["waterlevel1"].obj.setMap(null);
			}
		}
		if (selectedLayer == "globalcloud")
		{
			if($(this).prop("checked"))
			{
				cloudLayer.setMap(map);
			}
			else
			{
				cloudLayer.setMap(null);
			}
		}
		else
		{
			if($(this).prop("checked"))
			{
				waterresource[selectedLayer].obj.setMap(map);
				//console.log("water resources turned on");
			}
			else
			{
				waterresource[selectedLayer].obj.setMap(null);
			}
		}
	});
	$( "input.service_kml" ).on( "click", function() { 		//turn service layer on/off
		var selectedLayer = $(this).val();
		
		if($(this).prop("checked"))		// display selected layer			
		{	
			if (selectedLayer == 'effectivearea')			//call google maps utility library to overlay dynamic map service
			{
				var url = 'http://172.16.195.14/ArcGIS/rest/services/effective_area1/MapServer';
				effectivearea = new gmaps.ags.MapOverlay(url);
				effectivearea.setMap(map);
			}
			else if (selectedLayer == 'test')
			{
				var url = 'http://172.16.195.14/ArcGIS/rest/services/Public_water/MapServer';
				var agsType = new  gmaps.ags.MapType(url,{name:'publicwater', opacity:0.5});
				map.overlayMapTypes.insertAt(0, agsType);
			}
			else	
				displayKml(selectedLayer);					// display kml
					
		}
		else												// remove selected layer
		{
			if (selectedLayer == 'effectivearea')	
				effectivearea.setMap(null);
			else
				hideKml(selectedLayer);
		}
	});
	$( "input.branch_kml" ).on( "click", function() {		//turn engineering_kml -> branch kml on/off
		var branch = $(this).val();
		console.log(branch);
		if(branch_prev != null)
			for (var i = 0; i < branch_array[branch_prev].length - 1; i++){		//remove previous branch kmls
				branch_array[branch_prev][i].setMap(null);
			}
		if(branch_prev == branch){									// deselect radio button if clicked again
			$(this).prop('checked', false);
		}
		else{
			for (var i = 0; i < branch_array[branch].length - 1; i++){		//overlay all kmls in the selected branch
				branch_array[branch][i].setMap(map);
			}
			branch_prev = branch; 							// add current brach to memory for removal
		}
	});
	$( "#checnbox-select-traffic" ).on( "click", function() {				// display traffic layer
			
		if($(this).prop("checked"))
		{
			trafficLayer.setMap(map);
		}
		else
		{
			trafficLayer.setMap(null);
		}
	});
	
	$(function() {
		hideAddressBar();
	});
	$("#gsearch_button").on("click", function(){							
		var address = $('#gsearch_address').val();					// get keyword from user
		geocoder.geocode({											// send keyword as parameter to search location
			'address': address
		}, 
		function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				clearOverlays();
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});
				markersArray.push(marker);							// add search results to array for storage max=5 locations
				try{
					var marker2 = new google.maps.Marker({
						map: map,
						position: results[1].geometry.location
					});
					markersArray.push(marker2);
					
					var marker3 = new google.maps.Marker({
						map: map,
						position: results[2].geometry.location
					});
					markersArray.push(marker3);
					var marker4 = new google.maps.Marker({
						map: map,
						position: results[3].geometry.location
					});
					markersArray.push(marker4);
					var marker5 = new google.maps.Marker({
						map: map,
						position: results[4].geometry.location
					});
					markersArray.push(marker5);
				}
				catch(error){
					//onsole.log(error);
				}
				map.setCenter(results[0].geometry.location);
				
				map.setZoom(8);
				
			} else {												//popup if no results found
				$( "#noResults_popup" ).popup( "open" );
				console.log("No results found");
			}
		});
	});
	
	$("#logout").click(function() {									//logout session
		if (confirm('ยืนยันการออกจากระบบ') == true) {
			// alert('test');
			$.ajax({
				type: "POST",
				url: "Destroysession.php",
				data: {
					sure: "1",
				},
				statusCode: {
					404: function() {
						alert("??????????????????");
					},
					200: function() {
						// alert("?????????"); 
					}
				}
			}).done(function(data) {
				if (data == 1) {
					window.location = "LoginForm.php";
				}
			});
		}
	});
	$('.overflow_popup').on({									//set height of report popup
		
	  popupbeforeposition: function() {
		var maxHeight = $(window).height() - 50;
		
		$('.overflow_popup').css('max-height', maxHeight + 'px');
	  }
	})
	$("#report_popup").on({
		popupbeforeposition: function() {
			var maxHeight = $(window).height() - 60 + "px";
			var maxWidth = $(window).width() - 60 + "px";
			if(maxHeight > 640)									//fixed report popup to not exceed 640px
				maxHeight = 640;
			if(maxWidth > 640)
				maxWidth = 640;	
			htmlstring = "<iframe id='frame' src='http://gisonline.mwa.co.th/dss/graph/graph.php' width='"+maxWidth+"' height='"+maxHeight+"'></iframe>";
			//console.log("width= "+ ($(window).height() - 60) + " height= "+ ($(window).height() - 40));
			//console.log("REPORT width= "+maxHeight+ " height= "+maxWidth);
			$("#report_popup").html(htmlstring);
			//$("#frame").height = maxHeight+"";
			//$("#frame").width = String(maxWidth);
			//$("#report_popup").height = maxHeight;
			//$("#report_popup").width = maxWidth;
		}
		
	});
	
	
});